package cn.dansj.common.utils.transfer;

import cn.dansj.common.utils.enums.GlobalEnv;
import cn.dansj.common.utils.time.FastDateFormat;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public abstract class GetTime {
    public static String stringTime(String format) {
        return FastDateFormat.getInstance(format).format(new Date(System.currentTimeMillis()));
    }

    public static String stringTime(Long time) {
        return GlobalEnv.DateTimeFormatter.format(new Date(time));
    }

    public static String stringTime(Date time) {
        if (time == null) return "";
        return GlobalEnv.DateTimeFormatter.format(time);
    }

    public static String stringTime(String format, Long time) {
        return FastDateFormat.getInstance(format).format(new Date(time));
    }

    public static String stringTime(String format, Date time) {
        return FastDateFormat.getInstance(format).format(time);
    }

    public static String stringTime() {
        return GlobalEnv.DateTimeFormatter.format(new Date(System.currentTimeMillis()));
    }

    public static Date dateTime(String time) {
        if (Verification.checkNull(time)) return null;
        return Transformation.lambdaTryCatch(() -> GlobalEnv.DateFormatter.parse(time), () -> null);
    }

    public static Date timestamp(String time) {
        if (Verification.checkNull(time)) return null;
        return Transformation.lambdaTryCatch(() -> GlobalEnv.DateTimeFormatter.parse(time), () -> null);
    }

    public static Date maxTimeStamp() {
        return DateTime.getInstance(253402271999000L).toDate();
    }

    public static String nanoTime() {
        return String.valueOf(System.nanoTime());
    }

    public static Timestamp dbTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    public static Timestamp dbTimestamp(long time) {
        return new Timestamp(time);
    }

    public static List<Date> computeDateRange(String begDate, String endDate) {
        return computeDateRange(timestamp(begDate), timestamp(endDate));
    }

    public static List<Date> computeDateRange(Date date1, Date date2) {
        Date begDate;
        Date endDate;
        if (date1 == null) {
            date1 = new Date();
        }

        if (date2 == null) {
            date2 = new Date();
        }

        if (date1.compareTo(date2) > 0) {
            begDate = date2;
            endDate = date1;
        } else {
            begDate = date1;
            endDate = date2;
        }
        List<Date> rangeDate = new ArrayList<>();
        Calendar begCal = Calendar.getInstance();
        begCal.setTime(begDate);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        endCal.add(Calendar.DATE, 1);
        do {
            rangeDate.add(begCal.getTime());
            begCal.add(Calendar.DATE, 1);
        } while (endCal.after(begCal));
        return rangeDate;
    }
}
